import {Address} from '../../utils/address.js';
import {Order} from '../order/order-model.js';
import {Center} from './center-model.js';
import {Token} from '../../utils/token.js';
import { Packet } from '../packet/packet-model.js';
import { Member } from '../member/member-model.js';
import { Fenxiao } from '../fenxiao/fenxiao-model.js';

var packet = new Packet();
var address = new Address();
var order = new Order();
var center = new Center();
var token = new Token();
var member = new Member();
var fenxiao = new Fenxiao();
const app = getApp()
Page({
  data: {
    myorderGrids: [{
        "id":1,
        "icon": "icon-weibiaoti2fuzhi04",
        "labelName": "待付款"
      },
      {
        "id": 2,
        "icon": "icon-daishouhuo",
        "labelName": "待发货"
      },
      {
        "id": 3,
        "icon": "icon-daishouhuo",
        "labelName": "待收货"
      },
      {
        "id": 4,
        "icon": "icon-daipingjia",
        "labelName": "待评价"
      },
      {
        "id": 5,
        "icon": "icon-tuihuanhuo1",
        "labelName": "退还/售后"
      }
    ],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hasUserInfo: false,
    hidden: false,
    navHeight: app.globalData.navHeight,
    show_type: 1,  //1九宫格2多行
  },

  onLoad: function() {
    var that = this;
    //用户信息
    var args = {}
    member.info(args, (data) => {
      wx.setStorageSync('userInfo', data.item);
    });
  },
  onShow:function(){
    var that = this;
    //今日收益总额
    var args = {}
    fenxiao.dayMoney(args, (data) => {
      var userInfo = wx.getStorageSync('userInfo');
      userInfo.todayMoney = data.data;
      wx.setStorageSync('userInfo', userInfo);
      this.setData({
        todayMoney: data.data,
        userInfo: userInfo
      })
    });     
    //账户概况
    var args = {}
    center.index(args, (data) => {
      this.data.myorderGrids[0].num = data.item.num.ready_pay
      this.data.myorderGrids[1].num = data.item.num.ready_shipping
      this.data.myorderGrids[2].num = data.item.num.ready_confirm
      this.data.myorderGrids[3].num = data.item.num.ready_evalute
      that.setData({
        myorderGrids: this.data.myorderGrids,
        item: data.item
      })
    });      
  },

  // bindGetUserInfo: function(e) {
  //   var that = this;
  //     center._updateUserInfo(e.detail.userInfo, (data) => {
  //       wx.setStorageSync('userInfo', data.user);
  //       if (data.user){
  //         that.setData({
  //           userInfo: data.user,
  //           hidden: true
  //         })
  //       }
  //     });

  // },
  /**地址信息**/
  _getAddressInfo: function() {
    var that = this;
    address.getAddress((addressInfo) => {
      that._bindAddressInfo(addressInfo);
    });
  },
  /**红包退还**/
  back: function () {
    var that = this;
    var args = {};
    packet.back(args,(res) => {
      console.log(res);
    });
  },
  /*修改或者添加地址信息*/
  editAddress: function() {
    var that = this;
    that.setList(() => {
      that.chooseAddress();
    });
  },
  setList: function(func) {
    var that = this;
    wx.getSetting({ //先获取用户当前的设置  
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.authorize({ //使用这句话后wx.openSetting列表才会有选项
            scope: 'scope.address',
            success(res) {
              func(); //等同执行chooseAddress()方法
            },
            fail(res) {
              func(); //等同执行chooseAddress()方法
            }
          })
        } else {
          that.chooseAddress(); //如果用户已授权过地址
        }
      }
    })
  },
  chooseAddress: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.openSetting({})
        } else {
          wx.chooseAddress({
            success: function(res) {
              var addressInfo = {
                name: res.userName,
                mobile: res.telNumber,
                totalDetail: address.setAddressInfo(res)
              };
              if (res.telNumber) {
                that._bindAddressInfo(addressInfo);
                //保存地址
                address.submitAddress(res, (flag) => {
                  if (!flag) {
                    that.showTips('操作提示', '地址信息更新失败！');
                  }
                });
              }
              //模拟器上使用
              else {
                that.showTips('操作提示', '地址信息更新失败,手机号码信息为空！');
              }
            },
            fail: function(res) {
              console.log('操作提示', "未得到用户地址授权！");
            }
          })
        }
      }
    })
  },
  /*绑定地址信息*/
  _bindAddressInfo: function(addressInfo) {
    this.setData({
      addressInfo: addressInfo
    });
  },

  //跳转到全部订单页面
  myorders: function(event) {
    var index = center.getDataSet(event, 'index');
    index = parseInt(index) + 1;
    if (index == 5) {
      wx.navigateTo({
        url: '../refund/refund'
      })
    } else {
      wx.navigateTo({
        url: '../order/list?index=' + index
      })
    }
  },

  //跳转到商品收藏页
  toLists: function() {
    wx.navigateTo({
      url: '../collection/collection'
    })
  },

  //跳转到商品收藏页
  toVoteLists: function() {
    wx.navigateTo({
      url: '../article/vote-list'
    })
  },

  //跳转到购物车列表
  toCartList: function() {
    wx.switchTab({
      url: '../cart/cart'
    })
  },
  //跳转到积分列表页
  toJifenLists: function() {
    wx.navigateTo({
      url: '../center/jifenList'
    })
  },
  //跳转到钱包页
  toWallet:function(){
    wx.navigateTo({
      url: '../center/wallet'
    })
  },
  //跳转到卡券激活
  toCoupon: function () {
    wx.navigateTo({
      url: '../card/active'
    })
  },
  //跳转到红包记录
  toPacket: function () {
    wx.navigateTo({
      url: '../packet/log'
    })
  },
  //跳转到开通分销商
  open: function (e) {
    if(!this.data.userInfo.mobile){
      wx.navigateTo({
        url: '../member/binding?entry=1'
      })
    }else{
      wx.navigateTo({
        url: '../fenxiao/binding'
      })
    }
  },
  //跳转到商家管理平台
  shop: function () {
    if (this.data.item.has_shop == 0) {
      wx.navigateTo({
        url: '../shop_apply/index'
      })
    } else if (this.data.item.has_shop == 1) {
      wx.navigateTo({
        url: '../shop/index'
      })
    }
  },
  //获取用户授权信息
  bindGetUserInfo: function (e) {
    if (!e.detail.userInfo) {
      return false;
    }
    var that = this;
    center._updateUserInfo(e.detail.userInfo, (data) => {
      //data.user.username = data.user.nickName;
      if (data.user.status == 0) {
        wx.redirectTo({
          url: '/pages/home/forbidden',
        })
        return false;
      }
      wx.setStorageSync('userInfo', data.user);
      if (data.user) {
        that.setData({
          userInfo: data.user,
        })
      }
      let url = wx.getStorageSync('redirect-url');
      if (url) {
        wx.redirectTo({
          url: url,
        })
      }
    });
  },
})